package com.xiaoshuidi.cloud.module.finance.mq.producer;

import com.fasterxml.jackson.databind.node.LongNode;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Component
public class FinanceAccountBalanceProducer {
    @Resource
    private StreamBridge streamBridge;

    public void sendBalanceMessage(Long tenantId, BigDecimal balance, Set<Long> roomIds) {
        log.info("发送余额消息 {} {}", balance, roomIds);
        streamBridge.send("financeAccountBalance-out-0",
                MessageBuilder.withPayload(JsonUtils.getObjectMapper().createObjectNode()
                                .put("balance", balance)
                                .put("tenantId", tenantId)
                                .set("roomIds", JsonUtils.getObjectMapper()
                                        .createArrayNode().addAll(roomIds.stream()
                                                .map(LongNode::valueOf).collect(Collectors.toList()))).toString())
                        .setHeader(MessageConst.PROPERTY_TAGS, "BALANCE").build());
    }
}
